#pragma once
#include <functional>

namespace reactor
{
    class TcpServer;
    class Connection;

    using func_t = std::function<void(Connection*)>;
    
    class Connection
    {
        const static int default_val = -1;
    public:
        Connection(TcpServer* tsvp, int sock = default_val) 
        :_sock(sock) ,_tsvp(tsvp)
        {}
        ~Connection()
        {
            if(_sock != default_val)
            {
                close(_sock);
                logMessage(DEBUG, "close a connection, fd: %d", _sock);
            }
        }

        void Close()
        {
            close(_sock);
        }
        
        void Register(func_t r, func_t s, func_t e)
        {
            _recver = r;
            _sender = s;
            _excepter = e;
        }
    public:
        int _sock;
        func_t _recver;
        func_t _sender;
        func_t _excepter;
        std::string _inbuffer;
        std::string _outbuffer;

        TcpServer * _tsvp;
    };
}
